package com.example.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.adapter.GridViewAdapter;
import com.example.application.BaseActivity;
import com.example.application.MyApplication;
import com.example.bean.GridViewBean;
import com.example.db.DBService;
import com.example.zhaccount.MainActivity;
import com.example.zhaccount.R;

public class AddBillActivity extends BaseActivity implements OnClickListener{
	/**
	 * adapter 相关的变量
	 */
	private GridViewBean mBean;
	private GridViewAdapter mAdapter;
	private List<GridViewBean> mList;
	private Integer[] imgs;
	private String[] types;
	
	/**
	 * xml中的控件
	 */
	private GridView gridview;
	private ImageView imgBack;
	private TextView tvDate;
	private EditText edMark;
	private ImageView changeImg;
	private TextView changeName;
	private TextView changeBill;
	
	private static final int SHOW_DATE = 16;
	//GridView 的监听器
	private MyGVListener listener;
	private String localTime;
	
	/**
	 * 添加数据到数据库的变量
	 */
	private int mState = 1;
	private Intent intent;			//用来跳转的
	private String dbmoney;
	private String dbTime;
	private String dbMark;
	private GridViewBean dbGVbean;
	private int mindex = 0;
	private DBService dbService;
	private long e;
	/**
	 * 计算器相关变量
	 */
	private TextView one,four,seven,AC,two,five,eight,zero,three,six,nine,point,add,equ,ok;
	private boolean isChoosed = false;
	private String intext1 = null;
	private String intext2 = null;
	private String intext3 = null;
	private String intext4 = null;
	private String intext5 = null;
	private String intext6 = null;
	private String intext7 = null;
	private String intext8 = null;
	private String intext9 = null;
	private String intext0 = null;
	private String intextpoint = null;
	
	private double result =0;
	private double num1 = 0;
	private double num2 = 0;
	private int op = 0;
	
	private MyApplication myApp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addbill);
		
		mState = getIntent().getIntExtra("state", 1);
		myApp = (MyApplication) getApplication();
		
		findView();
		initCalculator();
		if (mState == 1) {
			initView();
		}else{
			initIncomeView();
		}
	}

	public void findView(){
		imgBack = (ImageView) findViewById(R.id.imgback);
		tvDate = (TextView) findViewById(R.id.date);
		edMark = (EditText) findViewById(R.id.mark);
		changeImg = (ImageView) findViewById(R.id.change_img);
		changeName = (TextView) findViewById(R.id.change_name);
		changeBill = (TextView) findViewById(R.id.change_bill);
		
		imgBack.setOnClickListener(this);
		tvDate.setOnClickListener(this);
		localTime = new SimpleDateFormat("yyyy-MM-dd")
						.format(new Date(System.currentTimeMillis()));
		tvDate.setText(localTime);
		
		gridview = (GridView) findViewById(R.id.gridview);
		listener = new MyGVListener();
		gridview.setOnItemClickListener(listener);
	}
	
	/**
	 * 初始化 支出 界面
	 */
	public void initView(){
		imgs = new Integer[]{R.drawable.type_big_1, R.drawable.type_big_2, R.drawable.type_big_3,
				R.drawable.type_big_4, R.drawable.type_big_5, R.drawable.type_big_6, R.drawable.type_big_7,
				R.drawable.type_big_8, R.drawable.type_big_9, R.drawable.type_big_10, R.drawable.type_big_11,
				R.drawable.type_big_12};
		types = new String[]{"一般", "用餐", "零食", "交通", "充值", "购物", "娱乐", "住房", "约会", "网购", "日用品", "香烟"};
		mList = new ArrayList<GridViewBean>();
		mBean = new GridViewBean(imgs, types);
		for (int i = 0; i < imgs.length; i++) {
			mList.add(mBean);
		}
		mAdapter = new GridViewAdapter(mList, this);
		gridview.setAdapter(mAdapter);
	}
	
	/**
	 * 初始化 收入界面
	 */
	public void initIncomeView(){
		imgs = new Integer[]{R.drawable.type_big_13, R.drawable.type_big_14, R.drawable.type_big_15,
				R.drawable.type_big_16, R.drawable.type_big_17, R.drawable.type_big_18, R.drawable.type_big_19};
		types = new String[]{"工资", "外快兼职", "奖金", "借入", "零花钱", "投资收入", "礼物红包"};
		mList = new ArrayList<GridViewBean>();
		mBean = new GridViewBean(imgs, types);
		for (int i = 0; i < imgs.length; i++) {
			mList.add(mBean);
		}
		mAdapter = new GridViewAdapter(mList, this);
		gridview.setAdapter(mAdapter);
	}
	
	
	public void initCalculator(){
		one = (TextView) findViewById(R.id.one);
		one.setOnClickListener(this);
		two = (TextView) findViewById(R.id.two);
		two.setOnClickListener(this);
		three = (TextView) findViewById(R.id.three);
		three.setOnClickListener(this);
		four = (TextView) findViewById(R.id.four);
		four.setOnClickListener(this);
		five = (TextView) findViewById(R.id.five);
		five.setOnClickListener(this);
		six = (TextView) findViewById(R.id.six);
		six.setOnClickListener(this);
		seven = (TextView) findViewById(R.id.seven);
		seven.setOnClickListener(this);
		eight = (TextView) findViewById(R.id.eight);
		eight.setOnClickListener(this);
		nine = (TextView) findViewById(R.id.nine);
		nine.setOnClickListener(this);
		zero = (TextView) findViewById(R.id.zero);
		zero.setOnClickListener(this);
		AC = (TextView) findViewById(R.id.clear);
		AC.setOnClickListener(this);
		add = (TextView) findViewById(R.id.add);
		add.setOnClickListener(this);
		equ = (TextView) findViewById(R.id.equ);
		equ.setOnClickListener(this);
		point = (TextView) findViewById(R.id.dian);
		point.setOnClickListener(this);
		ok = (TextView) findViewById(R.id.ok);
		ok.setOnClickListener(this);
		
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgback:
			finish();
			break;
		case R.id.date:
			showDialog(SHOW_DATE);
			break;
		case R.id.one:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext1 = changeBill.getText().toString();
			intext1 += "1";
			changeBill.setText(intext1);
			break;
		case R.id.two:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext2 = changeBill.getText().toString();
			intext2 += "2";
			changeBill.setText(intext2);
			break;
		case R.id.three:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext3 = changeBill.getText().toString();
			intext3 += "3";
			changeBill.setText(intext3);
			break;
		case R.id.four:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext4 = changeBill.getText().toString();
			intext4 += "4";
			changeBill.setText(intext4);
			break;
		case R.id.five:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext5 = changeBill.getText().toString();
			intext5 += "5";
			changeBill.setText(intext5);
			break;
		case R.id.six:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext6 = changeBill.getText().toString();
			intext6 += "6";
			changeBill.setText(intext6);
			break;
		case R.id.seven:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext7 = changeBill.getText().toString();
			intext7 += "7";
			changeBill.setText(intext7);
			break;
		case R.id.eight:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext8 = changeBill.getText().toString();
			intext8 += "8";
			changeBill.setText(intext8);
			break;
		case R.id.nine:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext9 = changeBill.getText().toString();
			intext9 += "9";
			changeBill.setText(intext9);
			break;
		case R.id.zero:
			if (isChoosed) {
				changeBill.setText(null);
				isChoosed = false;
			}
			intext0 = changeBill.getText().toString();
			intext0 += "0";
			changeBill.setText(intext0);
			break;
		case R.id.dian:
			intextpoint = changeBill.getText() + ".";
			changeBill.setText(intextpoint);
			break;
		case R.id.add:
			if (changeBill.getText().toString().equals(null)
					|| changeBill.getText().toString().equals("")) {
				return;
			}
			String s = changeBill.getText().toString();
			num1 = Double.valueOf(s);
			changeBill.setText(null);
			isChoosed = false;
			op = 1;
			break;
		case R.id.equ:
			if (changeBill.getText().toString().equals(null)
					|| changeBill.getText().toString().equals("")) {
				return;
			}
			String ss = changeBill.getText().toString();
			num2 = Double.valueOf(ss);
			if (op == 1) {
				result = num1 + num2;
				ss = String.format("%.2f",result);
			}
			changeBill.setText(String.valueOf(ss));
			isChoosed = true;
			op = 0;
			break;
		case R.id.clear:
			if (changeBill.getText().toString().equals(null)
					|| changeBill.getText().toString().equals("")) {
				return;
			}
			changeBill.setText(null);
			isChoosed = false;
			break;
		case R.id.ok:
			if (changeBill.getText().toString().equals(null)
					|| changeBill.getText().toString().equals("")) {
				return;
			}
			isChoosed = true;
			dbGVbean = new GridViewBean();
			dbmoney = changeBill.getText().toString();
			dbTime = tvDate.getText().toString();
			dbMark = edMark.getText().toString();
			dbGVbean.setMoney(dbmoney);
			dbGVbean.setImg(mindex);
			dbGVbean.setType(changeName.getText().toString());
			dbGVbean.setState(mState+"");
			dbGVbean.setTime(dbTime);
			dbGVbean.setMark(dbMark);
			dbGVbean.setUserName(myApp.getUserName());
			insertBill(dbGVbean);
			if (e != -1) {
				Toast.makeText(this, "insert success", 0).show();
			}else{
				Toast.makeText(this, "fail....", 0).show();
			}
			intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	
	 // 选择日期
	DatePickerDialog.OnDateSetListener onDateSetListener = new OnDateSetListener() {

		@SuppressWarnings("deprecation")
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
//			String time = new SimpleDateFormat("yyyy-MM-dd")
//								.format(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
			tvDate.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);
		}
	};

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		Calendar localCal = Calendar.getInstance();
		int i = localCal.get(Calendar.YEAR);
		int j = localCal.get(Calendar.MONTH);
		int k = localCal.get(Calendar.DAY_OF_MONTH);
		if (id == SHOW_DATE) {
			return new DatePickerDialog(this, onDateSetListener, i, j, k);
		}
		return null;
	}

	public void insertBill(GridViewBean gvb){
		dbService = new DBService(AddBillActivity.this);
		e = dbService.insertBill(dbGVbean);
	}
	
	
	/**
	 * GridView的监听函数
	 */
	private class MyGVListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			mBean = mList.get(position);
			changeIMGType(mBean,position);
			mindex = position;
		}
		
	}
	
	/**
	 * 展示 GridView的单击效果
	 */
	public void changeIMGType(GridViewBean bean,int position){
		Integer[] imgs = bean.getImgs();
		int path = imgs[position];
		changeImg.setImageResource(path);
		String[] str = bean.getTypes();
		String text = str[position];
		changeName.setText(text);
	}
	
}
